/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iwork.jute.compiler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
public class JRecord extends JCompType {

    private String mName;
    private String mModule;
    private List<JField> mFields;
    private List<JIndex> mIndices;

    /**
     * Creates a new instance of JRecord.
     */
    public JRecord(String name, ArrayList<JField> flist, ArrayList<JIndex> ilist) {
        super(name);
        int idx = name.lastIndexOf('.');
        mName = name.substring(idx + 1);
        mModule = name.substring(0, idx);
        mFields = flist;
        mIndices = ilist;
    }

    public String getName() {
        return mName;
    }


    public String getJavaPackage() {
        return mModule;
    }

    public String genLombokAnnotation() {
        String ret = "@Data\n";
        ret += "@AllArgsConstructor\n";
        ret += "@NoArgsConstructor\n";
        return ret;
    }

    public String generateJpaAnnotation() {
        return "  @ManyToOne\n";
    }

    public String genJpaAnnotation() {
        String ret = "@Entity\n";
        if (mIndices.isEmpty()) {
            return ret;
        }
        ret += "@Table(indexes={\n"
                + mIndices.stream().map(item -> item.genAnnotationDecl()).collect(Collectors.joining())
                + "})\n";
        return ret;
    }

    public void genJavaCode(File outputDirectory) throws IOException {
        String pkg = getJavaPackage();
        String pkgpath = pkg.replaceAll("\\.", "/");
        File pkgdir = new File(outputDirectory, pkgpath);
        if (!pkgdir.exists()) {
            // create the pkg directory
            if (!pkgdir.mkdirs()) {
                throw new IOException("Cannnot create directory: " + pkgpath);
            }
        } else if (!pkgdir.isDirectory()) {
            throw new IOException(pkgpath + " is not a directory.");
        }
        try (FileWriter jj = new FileWriter(new File(pkgdir, getName() + ".java"))) {
            jj.write("// File generated by Jute. Do not edit.\n");
            jj.write("/**\n");
            jj.write("* Licensed to the Apache Software Foundation (ASF) under one\n");
            jj.write("* or more contributor license agreements.  See the NOTICE file\n");
            jj.write("* distributed with this work for additional information\n");
            jj.write("* regarding copyright ownership.  The ASF licenses this file\n");
            jj.write("* to you under the Apache License, Version 2.0 (the\n");
            jj.write("* \"License\"); you may not use this file except in compliance\n");
            jj.write("* with the License.  You may obtain a copy of the License at\n");
            jj.write("*\n");
            jj.write("*     http://www.apache.org/licenses/LICENSE-2.0\n");
            jj.write("*\n");
            jj.write("* Unless required by applicable law or agreed to in writing, software\n");
            jj.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n");
            jj.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
            jj.write("* See the License for the specific language governing permissions and\n");
            jj.write("* limitations under the License.\n");
            jj.write("*/\n");
            jj.write("\n");
            jj.write("package " + getJavaPackage() + ";\n\n");
            jj.write("import lombok.*;\n");
            jj.write("import javax.persistence.*;\n");
            jj.write("import java.io.Serializable;\n\n");
            jj.write(genLombokAnnotation());
            jj.write(genJpaAnnotation());
            jj.write("public class " + getName() + " implements Serializable {\n");
            for (Iterator<JField> i = mFields.iterator(); i.hasNext(); ) {
                JField jf = i.next();
                jj.write(jf.genFieldAnnotation());
                jj.write(jf.genJavaDecl());
            }
            jj.write("}\n");
        }
    }
}
